package com.sihai.ehr.web.action.exam;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.ExamAwardItem;
import com.sihai.ehr.model.sys.ExamPageEnum;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.ExamAwardItemService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.action.invite.InviteFormAction;

/**
 * 考核奖惩控制器
 * 
 * @author henry
 * 
 */
@Results({
		@Result(name = "awarditem_list", location = "exam_awarditem_list.jsp"),
		@Result(name = "amerceitem_list", location = "exam_amerceitem_list.jsp"),
		@Result(name = "awarditem_manage", location = "exam_awarditem_manage.jsp"),
		@Result(name = "main", location = "exam_award_item_main.jsp") })
public class ExamAwardItemAction extends EHRAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Log log = LogFactory.getLog(ExamAwardItemAction.class);

	private List<Organization> organizations;
	@BeanInject
	private OrganizationService organizationService;
	@BeanInject
	private ExamAwardItemService examAwardItemService;

	private LimitedList<ExamAwardItem> awardItems;

	private ExamAwardItem examAwardItem;

	private Long awardItemId;
	
	private String ids;

	@Action("/exam/awarditem_main")
	@Override
	public String execute() throws Exception {
		listAction = "/exam/awarditem_list.xhtml";
		listTitle = "奖惩项目设定";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if (getCurrentUserId() > 0) {
				Long topOrgId = getCurrentUser().getWork().getOrganization()
						.getTopOrganization().getId();
				Iterator<Organization> it = organizations.iterator();
				while (it.hasNext()) {
					Organization o = it.next();
					if (o.getId() != topOrgId) {
						it.remove();
					}
				}
			}
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = false;
		return "main";
	}

	/**
	 * 获取奖励对象列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/exam/awarditem_list")
	public String awardList() throws Exception {
		awardItems = examAwardItemService.searchExamAwardItem(organizationId,
				0, createLimit(), createSorter());
		this.paging(awardItems);
		setPageUrl("exam/awarditem_list.xhtml");
		return "awarditem_list";
	}

	/**
	 * 获取惩罚对象列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/exam/amerceitem_list")
	public String amerceList() throws Exception {
		awardItems = examAwardItemService.searchExamAwardItem(organizationId,
				1, createLimit(), createSorter());
		this.paging(awardItems);
		setPageUrl("exam/amerceitem_list.xhtml");
		return "amerceitem_list";
	}

	@Action("/exam/awarditem_manage")
	public String dispatchAwardItemManage() throws Exception {
		if (awardItemId != null && awardItemId > 0) {
			// 修改
			examAwardItem = examAwardItemService.getExamAwardItem(awardItemId);
		} else {
			examAwardItem = new ExamAwardItem();
			int awardType = Integer.parseInt(getParameter("at") == null ? "0"
					: getParameter("at"));
			examAwardItem.setOrganization(organizationService.get(organizationId));
			examAwardItem.setAwardType(awardType);

		}
		return "awarditem_manage";
	}

	@Action("/exam/post_awarditem")
	public String postAwardItem() throws Exception {

		if (examAwardItem == null) {
			if (log.isErrorEnabled()) {
				log.equals("【奖惩对象为空】");
			}
			return ERROR;
		}
		if (examAwardItem.getAwardType() < 0) {
			if (log.isErrorEnabled()) {
				log.equals("【奖惩类型不能为空】");
			}
			return ERROR;
		}

		try {
			examAwardItemService.saveExamAwardItem(examAwardItem);
		} catch (Exception e) {
			// TODO: handle exception
		}

		return "main";
	}
	
	@Action(value = "/exam/awarditem_delete", results = { @Result(name = "success", type = "plainText") })
	public String deleteAwardItem() throws Exception {
		if (StringUtils.isNotBlank(ids)) {
			try {
				examAwardItemService.removeExamAwardItem(ids);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(e.getMessage());
				}
				return ERROR;
			}

		}

		return SUCCESS;
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}
	
	public void setAwardItems(LimitedList<ExamAwardItem> awardItems) {
		this.awardItems = awardItems;
	}
	
	public LimitedList<ExamAwardItem> getAwardItems() {
		return awardItems;
	}

	public void setExamAwardItem(ExamAwardItem examAwardItem) {
		this.examAwardItem = examAwardItem;
	}

	public ExamAwardItem getExamAwardItem() {
		return examAwardItem;
	}

	public void setAwardItemId(Long awardItemId) {
		this.awardItemId = awardItemId;
	}

	public Long getAwardItemId() {
		return awardItemId;
	}
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	
	public String getIds() {
		return ids;
	}
}
